//
//  XXdevice_Information.m
//  MacStockApp
//
//  Created by Andy MacPro on 12/11/22.
//
//

#import "XXdevice_Information.h"

@implementation XXdevice_Information
 

#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
//iOS
+ (NSString *)Device_UniqueIdentifier
{
    
    //取出device 資料
    UIDevice *device = [UIDevice currentDevice];
    
    NSString *device_uniqueIdentifier = [device uniqueIdentifier];
    /*
    NSString *device_name = [device name];
    NSString *device_systemName = [device systemName];
    NSString *device_systemVersion = [device systemVersion];
    NSString *device_model = [device model];
    NSString *device_localizedModel = [device localizedModel];
    */
    
    return device_uniqueIdentifier  ;
    
    
}

+ (NSString *)Device_name
{
    
    //取出device 資料
    UIDevice *device = [UIDevice currentDevice];
    
     
     
     NSString *device_name = [device name];
    /*
    NSString *device_systemName = [device systemName];
     NSString *device_systemVersion = [device systemVersion];
     NSString *device_model = [device model];
     NSString *device_localizedModel = [device localizedModel];
     */
    
    return device_name  ;
    
    
}

+ (NSString *)Device_systemName
{
    
    //取出device 資料
    UIDevice *device = [UIDevice currentDevice];
     
     
     
     NSString *device_systemName = [device systemName];
     /*
     NSString *device_systemVersion = [device systemVersion];
     NSString *device_model = [device model];
     NSString *device_localizedModel = [device localizedModel];
     */
    
    return device_systemName  ;
    
    
}

+ (NSString *)Device_model
{
    
    //取出device 資料
    UIDevice *device = [UIDevice currentDevice];
    
    
    
     
      
        NSString *device_model = [device model];
    /*
    NSString *device_localizedModel = [device localizedModel];
     */
    
    return device_model  ;
    
    
}

+ (NSString *)Device_systemVersion
{
    
    //取出device 資料
    UIDevice *device = [UIDevice currentDevice];
    
    
    
    
    NSString *device_systemVersion = [device systemVersion];
    /*
     NSString *device_model = [device model];
     NSString *device_localizedModel = [device localizedModel];
     */
    
    return device_systemVersion  ;
    
    
}

+ (NSString *)Device_localizedModel
{
    
    //取出device 資料
    UIDevice *device = [UIDevice currentDevice];
    
    
    
    
    
    
     NSString *device_localizedModel = [device localizedModel];
    
    
    return device_localizedModel  ;
    
    
}

#else
//OSX
+ (NSString *)Device_UniqueIdentifier
{
    
    NSArray * args = [NSArray arrayWithObjects:@"-rd1", @"-c", @"IOPlatformExpertDevice", @"|", @"grep", @"model", nil];
    NSTask * task = [[NSTask new] autorelease];
    [task setLaunchPath:@"/usr/sbin/ioreg"];
    [task setArguments:args];
    
    NSPipe * pipe = [[NSPipe new] autorelease];
    [task setStandardOutput:pipe];
    [task launch];
    
    NSArray * args2 = [NSArray arrayWithObjects:@"/IOPlatformUUID/ { split($0, line, \"\\\"\"); printf(\"%s\\n\", line[4]); }", nil];
    NSTask * task2 = [[NSTask new] autorelease];
    [task2 setLaunchPath:@"/usr/bin/awk"];
    [task2 setArguments:args2];
    
    NSPipe * pipe2 = [[NSPipe new] autorelease];
    [task2 setStandardInput:pipe];
    [task2 setStandardOutput:pipe2];
    NSFileHandle * fileHandle2 = [pipe2 fileHandleForReading];
    [task2 launch];
    
    NSData * data = [fileHandle2 readDataToEndOfFile];
    NSString * uuid = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    
    return uuid;
    
    
}
+ (NSString *)Device_Serial
{
    
    io_service_t    platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,
                                                                 
                                                                 IOServiceMatching("IOPlatformExpertDevice"));
    CFStringRef serialNumberAsCFString = NULL;
    
    if (platformExpert) {
        serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert,
                                                                 CFSTR(kIOPlatformSerialNumberKey),
                                                                 kCFAllocatorDefault, 0);
        IOObjectRelease(platformExpert);
    }
    
    NSString *serialNumberAsNSString = nil;
    if (serialNumberAsCFString) {
        serialNumberAsNSString = [NSString stringWithString:(NSString *)serialNumberAsCFString];
        CFRelease(serialNumberAsCFString);
    }
    
    return serialNumberAsNSString;
    
    
}
+ (NSString *)Device_SystemName{
    NSDictionary * sv = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
   return  [sv objectForKey:@"ProductName"];
}
+ (NSString *)Device_SystemVersion{
   
    //方法1
    SInt32 versionMajor=0, versionMinor=0, versionBugFix=0;
    Gestalt(gestaltSystemVersionMajor, &versionMajor);
    Gestalt(gestaltSystemVersionMinor, &versionMinor);
    Gestalt(gestaltSystemVersionBugFix, &versionBugFix);
    return  [NSString stringWithFormat:@"%d.%d.%d", versionMajor, versionMinor, versionBugFix];
    /*
     //方法2
     NSDictionary * sv = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
     device_systemVersion = [sv objectForKey:@"ProductVersion"];
     */

}
+ (NSString *)Device_Model{
    NSString *nsmodel;
    size_t len = 0;
    sysctlbyname("hw.model", NULL, &len, NULL, 0);
    if (len) {
        char *model = malloc(len*sizeof(char));
        sysctlbyname("hw.model", model, &len, NULL, 0);
        
        nsmodel= [NSString stringWithFormat:@"%s",model] ;
        
        free(model);
    }
    
    else{
                 
        nsmodel=@"";
        
    }
    
    return nsmodel;

}
+ (NSString *)RealTime_Account_ID{
    NSArray * args = [NSArray arrayWithObjects:@"-rd1", @"-c", @"IOPlatformExpertDevice", @"|", @"grep", @"model", nil];
    NSTask * task = [[NSTask new] autorelease];
    [task setLaunchPath:@"/usr/sbin/ioreg"];
    [task setArguments:args];
    
    NSPipe * pipe = [[NSPipe new] autorelease];
    [task setStandardOutput:pipe];
    [task launch];
    
    NSArray * args2 = [NSArray arrayWithObjects:@"/IOPlatformUUID/ { split($0, line, \"\\\"\"); printf(\"%s\\n\", line[4]); }", nil];
    NSTask * task2 = [[NSTask new] autorelease];
    [task2 setLaunchPath:@"/usr/bin/awk"];
    [task2 setArguments:args2];
    
    NSPipe * pipe2 = [[NSPipe new] autorelease];
    [task2 setStandardInput:pipe];
    [task2 setStandardOutput:pipe2];
    NSFileHandle * fileHandle2 = [pipe2 fileHandleForReading];
    [task2 launch];
    
    NSData * data = [fileHandle2 readDataToEndOfFile];
    NSString * uuid = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    
    
    NSString * String0 = [uuid substringWithRange:NSMakeRange(0, 1)];
    NSString * String3 = [uuid substringWithRange:NSMakeRange(3, 1)];
    NSString * String5 = [uuid substringWithRange:NSMakeRange(5, 1)];
    NSString * String7 = [uuid substringWithRange:NSMakeRange(7, 1)];
    NSString * String9 = [uuid substringWithRange:NSMakeRange(9, 1)];
    NSString * String10 = [uuid substringWithRange:NSMakeRange(10, 1)];
    NSString * String14 = [uuid substringWithRange:NSMakeRange(14, 1)];
    NSString * String15 = [uuid substringWithRange:NSMakeRange(15, 1)];
    NSString * String17 = [uuid substringWithRange:NSMakeRange(17, 1)];
    NSString * String19 = [uuid substringWithRange:NSMakeRange(19, 1)];
    NSString * String21 = [uuid substringWithRange:NSMakeRange(21, 1)];
    
    
    NSString * outs=[NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@",String0,String3,String5,String7,String9,String10,String14,String15,String17,String19,String21];
    
    
    return outs;

}


#endif

@end
